/*
		ServEngine framework library
		A library to fast develop JEE online applications
	
		Copyright 2008 José Ignacio de Córdoba Álvaro

		Licensed under the Apache License, Version 2.0 (the "License");
		you may not use this file except in compliance with the License.
		You may obtain a copy of the License at

				http://www.apache.org/licenses/LICENSE-2.0

		Unless required by applicable law or agreed to in writing, software
		distributed under the License is distributed on an "AS IS" BASIS,
		WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
		See the License for the specific language governing permissions and
		limitations under the License.
	
		Jose Ignacio de Cordoba Alvaro
		http://ignacio.decordoba.com
*/

package com.servengine.util;

public class DistributedFileManager extends File
{
	public DistributedFileManager(String path)
	{
		super(path);
		// Dirs are created as files ar added.
		/*for (int i=0; i<filedirs.length(); i++)
		{
			java.io.File dir = new java.io.File(this,""+filedirs.charAt(i));
			if (!dir.exists())
				dir.mkdirs();
		}*/
	}
	
	/**
	 * Moves supplied file to corresponding dir in this filemanager. File is moved using File.rename()
	 * @param file
	 */
	public void add(java.io.File file) //FIXME add a parameter to copy or move file (now just moves it) (!!!!)
	{
		File location = getLocation(file.getName());
		if (!location.exists())
			location.mkdirs();
		File newfile = new File(location,file.getName());
		if (newfile.exists())
			throw new IllegalArgumentException("There is already a file named "+file.getName()+" in this file manager");
		file.renameTo(newfile);
	}
	
	public void delete(String filename)
	{
	  File location = getLocation(filename);
	  if (!location.exists())
	    throw new IllegalArgumentException(filename+" doesn't even have a directory in this file manager");
	  File expurganda = new File(location,filename);
	  if (!expurganda.exists())
	    throw new IllegalArgumentException(filename+" doesn't exist in this file manager ("+location.getAbsolutePath()+")");
		expurganda.delete();
	}
	public File get(String filename)
	{
		return new File(getLocation(filename),filename);
	}
	private File getLocation(String filename)
	{
		return new File(this,filename.toLowerCase().substring(0,1));
	}
}
